package com.woniu.tms.attendance.controller;

import com.woniu.tms.entity.Sickform;
import com.woniu.tms.entity.User;
import com.woniu.tms.attendance.service.SickFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: peng
 */
@Controller
@RequestMapping("sickform")
public class SickFormController {
    @Autowired
    SickFormService sickFormService;

    /**
     * 初始化请假申请页面，填充信息
     * @param uid
     * @return
     */
    @RequestMapping("init")
    @ResponseBody
    public Map<String,Object> init(Integer uid){
        Map<String,Object> map = new HashMap<>();
        try{
            List<User> userList = sickFormService.listTmBySid(uid);
            User user = sickFormService.selectByUid(uid);
            map.put("result",true);
            map.put("userList",userList);
            map.put("user",user);

        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }finally {
            return map;
        }
    }

    /**
     * 新增请假信息
     * @param sickform
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public Map<String,Object> add(Sickform sickform){
        Map<String,Object> map = new HashMap<>();
        sickform.setStatus("待审批");
        try{
            sickFormService.add(sickform);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }finally {

        }
        return map;
    }


    /**
     * 初始化请假审批页面
     * @param confirmid
     * @return
     */
    @RequestMapping("initConfirm")
    @ResponseBody
    public Map<String,Object> initConfirm(Integer confirmid){
        Map<String,Object> map = new HashMap<>();

        try{
            List<Sickform> sickforms = sickFormService.listSickInfo(confirmid);
            map.put("sickforms",sickforms);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }finally {
            return map;
        }

    }

    /**
     * 更新请假信息状态；驳回或是通过
     * @param sid
     * @param status
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public Map<String,Object> update(Integer sid , String status){
        Map<String,Object> map = new HashMap<>();

        try{
            Sickform sickform = sickFormService.selectOne(sid);
            sickform.setStatus(status);
            sickFormService.update(sickform);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }finally {
            return map;
        }
    }

    /**
     * 学生根据自己的uid查询自己的历史请假信息
     * @param studentid
     * @param status
     * @return
     */
    @RequestMapping("myselfInfo")
    @ResponseBody
    public Map<String,Object> myselfInfo(Integer studentid , String status){
        Map<String,Object> map = new HashMap<>();

        try{
            List<Sickform> sickforms = sickFormService.listSickInfoByStudentid(studentid);
            map.put("result",true);
            map.put("sickforms",sickforms);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }finally {
            return map;
        }
    }


    /**
     * 删除单条历史请假信息
     * @param sid
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public Map<String,Object> delete(Integer sid){
        Map<String,Object> map = new HashMap<>();

        try{
            sickFormService.delete(sid);
            map.put("result",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("result",false);
        }finally {
            return map;
        }
    }


}
